
local Construct = require'constructs.construct'

local Ship = {}
Ship.mt = { __index = Ship }
setmetatable(Ship, Construct.mt)

function Ship:initialize(layout, origin_x, origin_y, offsets, wire_map)

	local ship = {}
	
	
	

	ship = Construct.new(layout, 'control_panel', origin_x, origin_y, wire_map)
	setmetatable(ship, Ship.mt)
	
	ship.offset_x = offsets.x
	ship.offset_y = offsets.y
	ship.type = "ship"
	
	ship.passengers = {}
	
	--self.id = ConstructManager:add_ship( self )
	--Construct.place_stations(ship)
	--Ship.man_stations(ship)
	
	return ship
end


function Ship.new( layout, origin_x, origin_y , offsets, wire_map)

	local o = Ship:initialize( layout, origin_x, origin_y, offsets, wire_map )

	return o

end

function Ship:update(dt)

	Construct.update(self, dt)
	
	local ship_upd = function(self, dt)
		for i = 1, #self.stations.cat do
			if self.stations.cat[i].update then
				self.stations.cat[i]:update(dt)
			end
		
		end
		
		for i = 1, #self.stations.fc do
			self.stations.fc[i]:update(dt)
		
		end
		coroutine.yield()
	
	end
	
	local a = MOAICoroutine.new()
	
	a:run(ship_upd, self, dt)
	

end


function Ship:docks( cons )


	Construct.docs(self, cons)

end





function Ship:add_passenger( passenger )
	local iter = #self.passengers+1
	passenger.passenger_id = iter
	self.passengers[iter] = passenger
	
	return iter

end

function Ship:remove_passenger( passenger )
	
	table.remove(self.passengers, passenger.passenger_id)
	passenger.passenger_id = 0

end

function Ship:man_stations()

	
	local stat = self.stations
	
	local a = 'test'
	
	print("SHIP man stations")
	
	
	
	if stat.cp then
		print("SHIP spawning captain")
		local capt = LifeManager.spawn_role(LifeManager, 'captain', 'human', self)
		capt.ship_id = self.ship_id		
		self:add_passenger(capt)
		capt:setLoc(0, 0)
		
	
	end
	

end




return Ship